#!/bin/sh

# $Id: mountall,v 1.4 2009-06-26 07:31:22 brian Exp $
# Copyright (c)2007 Brian Manning <brian at portaboom dot com>

# PLEASE DO NOT E-MAIL THE AUTHOR ABOUT THIS SOFTWARE
# The proper venue for questions is the LACK mailing list at:
# http://groups.google.com/linuxack or <linuxack.googlegroups.com>

# picks up things like colorize()
if ! [ -e $LACK_FUNCTIONS ]; then
    LACK_FUNCTIONS="/etc/scripts/lack_functions.sh"
fi # if ! [ -e $LACK_FUNCTIONS ]
source $LACK_FUNCTIONS

ACTION=$1
BINARY=/bin/mount
[ -x "$BINARY" ] || exit 1
BASENAME="squashfs_mountall"
# if the boot log is not set, then use a default logfile of /var/log/messages
if [ "x$DEBUG_LOG" = "x" ]; then
    LOGFILE="/var/log/messages"
else
    LOGFILE=$DEBUG_LOG
fi

## FUNC: mount_all_squashfs_packages
## ARG:  PKG_LIST, a list of squashfs packages to copy/mount
## DESC: Copies and mounts all squashfs (*.sfs) files found on to the ramdisk
## DESC: mounts the squashfs files, then creates symlink farms so that other
## DESC: binaries can use the mounted squashfs packages
mount_all_squashfs_packages () {
    local MOUNT_PATH=$1
    local PKG_LIST=$2
    colorize_nl $S_TIP "$BASENAME: Mounting all squashfs files in ${MOUNT_PATH}"
    for SFS_FILE in $PKG_LIST;
    do
        SFS_NAME=$(basename ${SFS_FILE} | sed 's/\.sfs$//')
        colorize $S_TIP "${BASENAME} - ${SFS_NAME}"

        #cp ${MOUNT_PATH}/portaboom/pkgs/$SFS_FILE /tmp
        if [ ! -d /pkgs/$SFS_NAME ]; then
            # no error checking here :/
            mkdir /pkgs/$SFS_NAME
        fi
        #mount -t squashfs -o loop /tmp/$SFS_FILE /pkgs/$SFS_NAME
        #mount -t squashfs -o loop ${MOUNT_PATH}/${SFS_FILE} /pkgs/$SFS_NAME
        mount -t squashfs -o loop ${SFS_FILE} /pkgs/$SFS_NAME
        check_exit_status $? "${BASENAME}: Mounting squashfs /pkgs/${SFS_NAME}"
    done # for SFS_FILE in $SFS_LIST
} # function mount_all_squashfs_packages

case "$ACTION" in
  vars)
    # echo out what commandline variables are parsed by this script
    echo "${BASENAME}:"
    exit 0
  ;;
  start)
    mkdir /pkgs
    colorize_nl $S_TIP "$BASENAME: Checking for CD-ROM Devices..."
    CDROM_DEV=$(/sbin/udevadm info --query=name --name=cdrom)
    if [ $? -gt 0 ]; then
        colorize_nl "$BASENAME: udevadm can't find CD-ROM device;"
        colorize_nl "$BASENAME: skipping CD-ROM mounting"
    else
        colorize $S_TIP "$BASENAME: Mounting /dev/$CDROM_DEV"
        $BINARY -t iso9660 -o ro /dev/$CDROM_DEV /mnt/cdrom 2>/dev/null
        check_exit_status $? "$BASENAME: Mounting /dev/$CDROM_DEV"
        if [ $? -eq 0 ]; then
            # the mount worked, so run a find on the CD-ROM drive now
            SFS_LIST=$(find /mnt/cdrom/portaboom/pkgs -type f -iname "*.sfs")
            mount_all_squashfs_packages "/mnt/cdrom" "${SFS_LIST}"
        fi # if [ $? -eq 0 ]
    fi # if [ $? -gt 0 ];

    colorize_nl $S_TIP "$BASENAME: Checking for Disk/Thumbdrive Devices..."
    for DISK in sda sdb sdc sdd sde sdf sdg hda hdb hdc hdd; do
        # skip testing the CD-ROM device
        if [ "x${DISK}" = "x${CDROM_DEV}" ]; then
            continue
        fi
        # skip devices that are already mounted for whatever reason
        if [ $(/bin/grep -c $DISK /proc/mounts) -gt 0 ]; then
            continue
        fi
        # we only care about the exit code; redirect output to /dev/null
        /sbin/udevadm info --query=name --name=${DISK} >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            # get a list of partitions that we can try to mount
            PARTITIONS=$(cat /proc/partitions | /bin/grep ${DISK} \
                | /usr/bin/awk '{print $4}')
            # enumerate through the list of partitions to try and mount
            for MNT_POINT in $(echo ${PARTITIONS}); do
                # don't create a directory if one already exists
                if [ ! -d /mnt/$MNT_POINT ]; then
                    colorize $S_TIP "$BASENAME: mkdir /mnt/${MNT_POINT}"
                    mkdir /mnt/$MNT_POINT
                    check_exit_status $? "${BASENAME}: mkdir /mnt/${MNT_POINT}"
                fi
                colorize $S_TIP "$BASENAME: Mounting /dev/${MNT_POINT}"
                $BINARY -t auto -o ro /dev/$MNT_POINT \
                    /mnt/$MNT_POINT >> $LOGFILE 2>&1
                check_exit_status $? "${BASENAME}: mounting /dev/${MNT_POINT}"
                if [ $? -eq 0 ]; then
                    # FIXME
                    # - this doesn't adhere to the list of directories to
                    # search, this is catching everything on the drive :(
                    SFS_LIST=$(find /mnt/${MNT_POINT}/portaboom/pkgs \
                        -type f -iname "*.sfs" | sort)
                    # try and mount all of the packages we find
                    mount_all_squashfs_packages \
                        "/mnt/${MNT_POINT}/portaboom/pkgs" \
                        "${SFS_LIST}"
                fi # if [ $? -eq 0 ]
            done # for MNT_POINT in $(echo ${PARTITIONS});
        fi # if [ $? -eq 0 ];
    done # for DISK in hda hdb hdc hdd sda sdb sdc sdd sde sdf sdg
    ;; # start)
  stop)
    #colorize $S_TIP "Unmounting all filesystems"
    echo "Unmounting all device-backed filesystems"
    for MOUNT in $(cat /proc/mounts | grep "squash" | awk '{print $1}' );
    do
        echo " - $MOUNT"
        /bin/umount $MOUNT
        check_exit_status $? "${BASENAME}: umount ${MOUNT}"
    done
    ;; # stop)
  restart|force-reload)
     $0 stop
     $0 start
    ;;
  *)
    echo "Usage: ${BASENAME} {start|stop|restart|force-reload}" >&2
    exit 3
    ;;
esac

exit 0
# vi: set shiftwidth=4 tabstop=4 filetype=sh :
# конец!
